﻿#include <string>
#include "proto_package.h"
#include "common.pb.h"
#include "SeqId.h"
#include "Head.h"
#include "CmdId.h"
#include "device_request_distribute.pb.h"
#include "file_upload_check.pb.h"
#include "device.pb.h"
using namespace QjProtoCommon;
using namespace std;
#define  MSG_PACKET(protoMsg,msgCmd,moudleid,rsp)\
{SBuf contx;\
protoMsg.SerializeToArray(contx.data(), contx._size - CHead::GetHeadLen());\
contx._e += protoMsg.ByteSizeLong();\
CHead packhead(msgCmd, contx.len());\
packhead.m_nAttach1 = moudleid;\
SBuf msg;\
if (RERROR == packhead.PackHead(msg.data(), CHead::GetHeadLen()))\
{\
	LOG_ERROR("PackHead failed");\
	return;\
}\
msg._e += CHead::GetHeadLen();\
msg.append(contx.data(), contx.len());\
rsp->append(msg.data(), msg.len());}\

CProtoPackage::CProtoPackage()
{
}


CProtoPackage::~CProtoPackage()
{
}




void CProtoPackage::PackHeartbeatReqMsg(OUT SBuf*  rsp)
{	
	HeartBeatReq heartbeat;
	std::string seqId;
	if (CSeqId::GetSid(0, seqId) != ROK)
	{
		LOG_ERROR("GetSid failed");
		return;
	}
	heartbeat.set_seqid(seqId);

	SBuf contx;
	heartbeat.SerializeToArray(contx.data(), contx._size - CHead::GetHeadLen());
	contx._e += heartbeat.ByteSizeLong();

	CHead packhead(E_CMD_CODE_HEART_REQ, contx.len());
	SBuf msg;
	if (RERROR == packhead.PackHead(msg.data(), CHead::GetHeadLen()))
	{
		LOG_ERROR("PackHead failed");
		return;
	}
	msg._e += CHead::GetHeadLen();
	msg.append(contx.data(), contx.len());
	rsp->append(msg.data(), msg.len());
}

void CProtoPackage::PackHeartbeatRspMsg(std::string seqId, uint16_t retCode,OUT SBuf*  rsp)
{
	HeartBeatRes heartbeat;	
	heartbeat.set_seqid(seqId);
	heartbeat.set_rescode(retCode);
	SBuf contx;
	heartbeat.SerializeToArray(contx.data(), contx._size - CHead::GetHeadLen());
	contx._e += heartbeat.ByteSizeLong();

	CHead packhead(E_CMD_CODE_HEART_RES, contx.len());
	SBuf msg;
	if (RERROR == packhead.PackHead(msg.data(), CHead::GetHeadLen()))
	{
		LOG_ERROR("PackHead failed");
		return;
	}
	msg._e += CHead::GetHeadLen();
	msg.append(contx.data(), contx.len());
	rsp->append(msg.data(), msg.len());
}


void CProtoPackage::PackDevMngSvrRegisteMsg(IN uint64_t moudleid, uint64_t deviceManageId, OUT SBuf*  rsp)
{
	QjDeviceQueryDistribute::DeviceManageRegisterReq devMngReq;

	std::string seqId;
	if (CSeqId::GetSid(0, seqId) != ROK)
	{
		LOG_ERROR("GetSid failed");
		return;
	}
	devMngReq.set_seqid(seqId);
	devMngReq.set_devicemanageid(deviceManageId);
	SBuf contx;
	devMngReq.SerializeToArray(contx.data(), contx._size - CHead::GetHeadLen());
	contx._e += devMngReq.ByteSizeLong();

	CHead packhead(E_CMD_CODE_DEVICEMANAGEREGISTER_REQ, contx.len());
	packhead.m_nAttach1 = moudleid;
	SBuf msg;
	if (RERROR == packhead.PackHead(msg.data(), CHead::GetHeadLen()))
	{
		LOG_ERROR("PackHead failed");
		return;
	}
	msg._e += CHead::GetHeadLen();
	msg.append(contx.data(), contx.len());
	rsp->append(msg.data(), msg.len());
}

void CProtoPackage::PackDevMngSvrRegisterRspMsg(IN std::string strSeqId, uint16_t retCode, OUT SBuf*  rsp)
{
	QjDeviceQueryDistribute::DeviceManageRegisterRes devMngRegrsp;
	devMngRegrsp.set_seqid(strSeqId);
	devMngRegrsp.set_rescode(retCode);

	SBuf contx;
	devMngRegrsp.SerializeToArray(contx.data(), contx._size - CHead::GetHeadLen());
	contx._e += devMngRegrsp.ByteSizeLong();
	CHead packhead(E_CMD_CODE_DEVICEMANAGEREGISTER_RES, contx.len());
	SBuf msg;
	if (RERROR == packhead.PackHead(msg.data(), CHead::GetHeadLen()))
	{
		LOG_ERROR("PackHead failed");
		return;
	}	
	msg._e += CHead::GetHeadLen();
	msg.append(contx.data(), contx.len());
	rsp->append(msg.data(), msg.len());
}

void CProtoPackage::PackBackSvrRegisterMsg(IN uint64_t moudleid, OUT SBuf*  rsp)
{
	std::string seqId;
	if (CSeqId::GetSid(0, seqId) != ROK)
	{
		LOG_ERROR("GetSid failed");
		return;
	}

	QjDeviceQueryDistribute::ServerRegisterReq svrReg;
	svrReg.set_seqid(seqId);
	svrReg.set_moduleid(moudleid);

	SBuf contx;
	svrReg.SerializeToArray(contx.data(), contx._size - CHead::GetHeadLen());
	contx._e += svrReg.ByteSizeLong();
	CHead packhead(E_CMD_CODE_SERVERREGISTER_REQ, contx.len());
	packhead.m_nAttach1 = moudleid;
	SBuf msg;
	if (RERROR == packhead.PackHead(msg.data(), CHead::GetHeadLen()))
	{
		LOG_ERROR("PackHead failed");
		return;
	}	
	msg._e += CHead::GetHeadLen();
	msg.append(contx.data(), contx.len());
	rsp->append(msg.data(), msg.len());
}


void CProtoPackage::PackBackSvrRegisterRspMsg(IN std::string strSeqId, uint16_t retCode, OUT SBuf*  rsp)
{
	QjDeviceQueryDistribute::ServerRegisterRes svrRegrsp;
	svrRegrsp.set_seqid(strSeqId);
	svrRegrsp.set_rescode(retCode);

	SBuf contx;
	svrRegrsp.SerializeToArray(contx.data(), contx._size - CHead::GetHeadLen());
	contx._e += svrRegrsp.ByteSizeLong();
	CHead packhead(E_CMD_CODE_SERVERREGISTER_RES, contx.len());
	SBuf msg;
	if (RERROR == packhead.PackHead(msg.data(), CHead::GetHeadLen()))
	{
		LOG_ERROR("PackHead failed");
		return;
	}
	msg._e += CHead::GetHeadLen();
	msg.append(contx.data(), contx.len());
	rsp->append(msg.data(), msg.len());
}


void CProtoPackage::PackStopVedioUploadReq(IN uint64_t moudleid,IN uint64_t sessionid, OUT SBuf*  rsp)
{
	std::string seqId;
	if (CSeqId::GetSid(0, seqId) != ROK)
	{
		LOG_ERROR("GetSid failed");
		return;
	}
	QjDevice::StartVedioUploadReq req;
	req.set_seqid(seqId);
	req.set_sessionid(std::to_string(sessionid).c_str(), std::to_string(sessionid).length());
	LOG_INFO("PackStopVedioUploadReq sessionid %s %u ", std::to_string(sessionid).c_str(), sessionid);
	SBuf contx;
	req.SerializeToArray(contx.data(), contx._size - CHead::GetHeadLen());
	contx._e += req.ByteSizeLong();
	CHead packhead(E_CMD_CODE_STOPVEDIOUPLOAD_REQ, contx.len());
	packhead.m_nAttach1 = moudleid;
	SBuf msg;
	if (RERROR == packhead.PackHead(msg.data(), CHead::GetHeadLen()))
	{
		LOG_ERROR("PackHead failed");
		return;
	}
	msg._e += CHead::GetHeadLen();
	msg.append(contx.data(), contx.len());
	rsp->append(msg.data(), msg.len());
}

void CProtoPackage::PackStopVedioUploadRsp(IN uint64_t sessionid,IN std::string strSeqId, IN uint16_t retCode, OUT SBuf*  rsp)
{	
	QjDevice::StartVedioUploadRes uploadrsp;
	uploadrsp.set_seqid(strSeqId); 
	uploadrsp.set_rescode(retCode);

	SBuf contx;
	uploadrsp.SerializeToArray(contx.data(), contx._size - CHead::GetHeadLen());
	contx._e += uploadrsp.ByteSizeLong();
	CHead packhead(E_CMD_CODE_STOPVEDIOUPLOAD_RES, contx.len());
	packhead.m_nAttach1 = sessionid;
	SBuf msg;
	if (RERROR == packhead.PackHead(msg.data(), CHead::GetHeadLen()))
	{
		LOG_ERROR("PackHead failed");
		return;
	}
	msg._e += CHead::GetHeadLen();
	msg.append(contx.data(), contx.len());
	rsp->append(msg.data(), msg.len());
	LOG_INFO("PackStopVedioUploadRsp sessionid 0x%0x", sessionid);
}
void CProtoPackage::PackFileCheckReq(IN uint64_t moudleid, OUT SBuf*  rsp)
{
	QjFileUploadCheck::FileUploadReq fileCheck;
	fileCheck.set_deviceno(string("0001"));
	fileCheck.set_filepath(string("/tmp/nfs/audio.raw.aac"));
	fileCheck.set_type(QjFileUploadCheck::FileType::FILETYPE_RECORD);
	fileCheck.set_channel(QjFileUploadCheck::ChannelType::CHANNELTYPE_MIX);
	fileCheck.set_timetableid(1);
	fileCheck.set_starttime(string("2019-07-5"));
	fileCheck.set_stoptime(string("2019-07-2"));
	fileCheck.set_checksum(string("afbabf1f2fc829254f33f44e544f7081"));	
	MSG_PACKET(fileCheck, E_CMD_CODE_FILEUPLOADRESULT_REQ, moudleid,rsp);
}
